<?php

/**
 * Entry actions.
 *
 * @package    ProdAssistant
 * @subpackage Entry
 * @author     Paweł Świtalla
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EntryActions extends sfActions
{
  private $core;
  public function preExecute()
  {
    $this->core = PACore::getInstance();
  }
  
  // User
  public function executeUserList(sfWebRequest $request)
  {
    $this->users = Doctrine_Core::getTable('User')->findAll();
  }
  
  public function executeShowUser(sfWebRequest $request)
  {
    
  }
  
  public function executeDeleteUser(sfWebRequest $request)
  {
    $id = $request->getParameter('id') ? : false;
    if($id)
    {
      $user = Doctrine_Core::getTable('User')->find($id);
      if($user instanceof User)
      {
        $user->delete();
        $this->redirect($request->getReferer());
      }
    }
  }
  
  public function executeCreateUser(sfWebRequest $request)
  {
    $this->form = new UserForm();
    $url = $this->core->generateAppUrl('Core', 'userList');
    
    if($request->isMethod('post'))
    {
      $parameters = $request->getParameter('user');
      $this->form->bind($parameters);
      if($this->form->isValid())
      {
        $user = $this->form->save();
        $user->salt = md5(time());
        $user->password = md5($user->salt.$user->password);
        $user->directory = "/{$user->getLogin()}_".substr($user->getSalt(), 0, 6);
        $user->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  public function executeUpdateUser(sfWebRequest $request)
  {
    $user = Doctrine_Core::getTable('User')->find($request->getParameter('id'));
    $this->form = new UserForm($user);
    $url = $this->core->generateAppUrl('Core', 'userList');

    if($request->isMethod('put'))
    {
      $parameters = $request->getParameter('user');
      $this->form->bind($parameters);
      if($this->form->isValid())
      {
        $user = $this->form->save();
        $user->password = md5($user->salt.$user->password);
        $user->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  // Role
  public function executeShowRole(sfWebRequest $request)
  {
    
  }
  
  public function executeRoleList(sfWebRequest $request)
  {
    $this->roles = Doctrine_Core::getTable('Role')->findAll();
    $this->setLayout('clear');
  }
  
  public function executeDeleteRole(sfWebRequest $request)
  {
    $id = $request->getParameter('id') ? : false;
    if($id)
    {
      $role = Doctrine_Core::getTable('Role')->find($id);
      if($role instanceof Role)
      {
        $role->delete();
        $this->setLayout('clear');
        $this->redirect($request->getReferer());
      }
    }
  }
  
  public function executeCreateRole(sfWebRequest $request)
  {
    $this->form = new RoleForm();
    //$url = $this->core->generateAppUrl('Core', 'roleList');
    $url = $request->getReferer();
    
    if($request->isMethod('post'))
    {
      $parameters = $request->getParameter('role');
      $this->form->bind($parameters);
      if($this->form->isValid())
      {
        $item = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  public function executeUpdateRole(sfWebRequest $request)
  {
    $role = Doctrine_Core::getTable('Role')->find($request->getParameter('id'));
    $this->form = new RoleForm($role);
    //$url = $this->core->generateAppUrl('Core', 'roleList');
    $url = $request->getReferer();
    
    if($request->isMethod('put'))
    {
      $parameters = $request->getParameter('role');
      $this->form->bind($parameters);
      if($this->form->isValid())
      {
        $item = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($url);
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  
  // Rest
  
  public function executeError404(sfWebRequest $request)
  {
  }
  
  public function executeIndex(sfWebRequest $request)
  {
    //$core = PACore::getInstance();
    //$this->redirect($core->generateAppUrl('Store', 'categoryHomepage'));
  }  
  
  public function executeGetForm(sfWebRequest $request)
  {
    $this->isAjax = $request->isXmlHttpRequest();
    $this->formName = $request->getParameter('form');
    $this->action = $request->getParameter('form_action');
    $id = $request->getParameter('id') ? : false;
    $restParameters = (array) $request->getParameterHolder()->getAll();
    $model = str_replace('Form', '', $this->formName);
    $routes = PACore::loadYml('forms_routes');
    $routeParameters = array();
    $record = false;
    
    if($id)
    {
      $routeParameters['id'] = $id;
      $record = Doctrine_Core::getTable($model)->find($id);
    }
    
    if($model == 'StoreItem')
    {
      if($record)
      {
        $this->form = new $this->formName($restParameters['category'], null, $record);
      }
      else
      {
        $this->form = new $this->formName($restParameters['category']);
      }
      $routeParameters['category'] = $restParameters['category'];
    }
    else
    {
      if($record)
      {
        $this->form = new $this->formName($record);
      }
      else $this->form = new $this->formName;
    }
    
    if(count($routes))
    {
      $this->route = $this->core->generateAppUrl($routes[$model]['_module'], $routes[$model][$this->action], false, $routeParameters);
      $this->formId = $routes[$model][$this->action];
    }
  }
  
  public function executeGetComponent(sfWebRequest $request)
  {
    $this->module = $request->getParameter('componentModule');
    $this->component = $request->getParameter('name');
    $this->parameters = array('handler' => $request->getParameter('handler'));
    $this->setLayout('clear');
  }
  
  public function executeGetRoute(sfWebRequest $request)
  {
    $routeApp = $request->getParameter('routeApp');
    $routeName = $request->getParameter('routeName');
    
    if($request->hasParameter('parameters'))
    {
      return $this->core->generateAppUrl($routeApp, $routeName, true, $request->getParameter('parameters'));
    }
    else
    {
      return $this->core->generateAppUrl($routeApp, $routeName);
    }
  }
}
